package cn.edu.sgu.www.mhxysy.service.role.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.role.RoleCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.role.RoleCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.role.RoleCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.role.RoleCategoryPager;
import cn.edu.sgu.www.mhxysy.redis.repository.impl.RoleCategoryRepository;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.role.RoleCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class RoleCategoryServiceImpl implements RoleCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final RoleCategoryMapper roleCategoryMapper;
	private final RoleCategoryRepository roleCategoryRepository;

	@Autowired
	public RoleCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			RoleCategoryMapper roleCategoryMapper,
			RoleCategoryRepository roleCategoryRepository) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.roleCategoryMapper = roleCategoryMapper;
		this.roleCategoryRepository = roleCategoryRepository;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_JS;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(RoleCategoryDTO roleCategoryDTO) {
		RoleCategory roleCategory = toEntity(roleCategoryDTO);

		roleCategory.setId(null);

		roleCategoryMapper.insert(roleCategory);
	}

	@Override
	public void updateById(RoleCategoryDTO roleCategoryDTO) {
		ObjectUtils.requireNonNull(roleCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

		RoleCategory roleCategory = toEntity(roleCategoryDTO);

		roleCategoryMapper.updateById(roleCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<RoleCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				roleCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<RoleCategory> selectAll() {
		return roleCategoryRepository.selectList();
	}

	@Override
	public RoleCategory selectById(Integer id) {
		return roleCategoryRepository.selectById(id.toString());
	}

	@Override
	public Page<RoleCategory> selectByPage(RoleCategoryPager pager) {
		QueryWrapper<RoleCategory> queryWrapper = new QueryWrapper<>();
		Page<RoleCategory> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getPhyle() != null,
				"phyle", pager.getPhyle()
		);
		queryWrapper.eq(
				pager.getGender() != null,
				"gender", pager.getGender()
		);

		return roleCategoryMapper.selectPage(page, queryWrapper);
	}

	@Override
	public List<RoleCategory> selectList(Integer phyle, Integer gender) {
		QueryWrapper<RoleCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq(phyle != null, "phyle", phyle);
		queryWrapper.eq(gender != null, "gender", gender);

		queryWrapper.orderByAsc("phyle", "gender");

		return roleCategoryMapper.selectList(queryWrapper);
	}

	private RoleCategory toEntity(RoleCategoryDTO roleCategoryDTO) {
		Integer id = roleCategoryDTO.getId();
		String name = roleCategoryDTO.getName();

		QueryWrapper<RoleCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.ne(id != null, "id", id);
		queryWrapper.eq("name", name);

		Long count = roleCategoryMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "角色名重复");
		}

		RoleCategory roleCategory = new RoleCategory();

		roleCategory.setId(id);
		roleCategory.setName(name);
		roleCategory.setNote(roleCategoryDTO.getNote());
		roleCategory.setPhyle(roleCategoryDTO.getPhyle());
		roleCategory.setGender(roleCategoryDTO.getGender());
		roleCategory.setEquipmentId(roleCategoryDTO.getEquipmentId());

		return roleCategory;
	}

}